package vn.nt.healthcare.server.ws.service.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import vn.nt.healthcare.server.ws.domain.Company;
import vn.nt.healthcare.server.ws.orm.hibernate.HibernateUtil;

public final class ServiceHelper {

	public static Session openSession(Company company) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory(
				company.getDb_host(), company.getDb_port(),
				company.getDb_name(), company.getDb_user(),
				company.getDb_pass());
		Session session = sessionFactory.openSession();
		return session;
	}

	public static Session getSession(Company company) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory(
				company.getDb_host(), company.getDb_port(),
				company.getDb_name(), company.getDb_user(),
				company.getDb_pass());
		Session session = sessionFactory.getCurrentSession();
		return session;
	}

	public static void removeSession(Company company) {
		HibernateUtil.removeSessionFactory(company.getDb_host(),
				company.getDb_name(), company.getDb_user(),
				company.getDb_pass());
	}

	public static void closeSession(Session session) {
		try {
			if (session != null) {
				session.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
